/**
YASTACS startmenu module.

License: GNU General Public License

YASTACS game
Copyright (C) 2012 Kalman Kiss, kiskami@freemail.hu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Contact author at: kiskami@freemail.hu or
Hungary, 8900 Zalaegerszeg
Kakukkfu u 4.
*/
module startmenu.StartMenu;

import std.stdio:writeln;
import std.conv:to;

import iCore;
import Common;

import ogreint.KeyCode;
import ogreint.Timer;
import ceguiint.Window;
import ceguiint.Combobox;
import ceguiint.ListboxTextItem;

import rendering.CameraYaw;

import client.Client;
import server.SingleUserGameServer;

immutable string mainmenu_layout = "mainmenu.layout";
immutable string newgame_layout = "newgame.layout";

immutable string mainmenu_newgamebtn = "MainMenuRoot/newgamebutton";
immutable string mainmenu_newmultigamebtn = "MainMenuRoot/newmultiplayergamebutton";
immutable string mainmenu_loadgamebtn = "MainMenuRoot/loadgamebutton";
immutable string mainmenu_optionsbtn = "MainMenuRoot/optionsbutton";
immutable string mainmenu_exitbtn = "MainMenuRoot/exitbutton";

immutable string newgame_nameeditbox = "NewGameRoot/nameeditbox";
immutable string newgame_seededitbox = "NewGameRoot/seededitbox";
immutable string newgame_gametypecombobox = "NewGameRoot/GameTypeCB";
immutable string newgame_violencecheckbox = "NewGameRoot/violencecheckbox";

immutable string newgame_backbtn = "NewGameRoot/backbutton";
immutable string newgame_startgamebtn = "NewGameRoot/startgamebutton";

class StartMenu : GameState {

	this(iCore core_) {
		debug writeln("StartMenu ctor");
		core = core_;
		state = State.CREATED;
		runstate = RunState.NONE;

		// load and setup menu elements
		startmenu_window = core.gui.loadLayout(mainmenu_layout);
		newgame_window = core.gui.loadLayout(newgame_layout);

		core.gui.setWidgetText("MainMenuRoot/version","version "~ver);
		core.gui.setWidgetText("MainMenuRoot/copyright",copyright);

		// menuitems not yet implemented
		core.gui.setWidgetEnabled(mainmenu_newmultigamebtn,false);
		core.gui.setWidgetEnabled(mainmenu_loadgamebtn,false);
		core.gui.setWidgetEnabled(mainmenu_optionsbtn,false);

		core.gui.subscribePushButtonEventClicked(mainmenu_newgamebtn, &this.newgame_clicked);
		core.gui.subscribePushButtonEventClicked(mainmenu_newmultigamebtn, &this.newmultigame_clicked);
		core.gui.subscribePushButtonEventClicked(mainmenu_loadgamebtn, &this.loadgame_clicked);
		core.gui.subscribePushButtonEventClicked(mainmenu_optionsbtn, &this.options_clicked);
		core.gui.subscribePushButtonEventClicked(mainmenu_exitbtn, &this.exit_clicked);

		core.gui.subscribePushButtonEventClicked(newgame_backbtn, &this.newgame_backbtn_clicked);
		core.gui.subscribePushButtonEventClicked(newgame_startgamebtn, &this.newgame_startgamebtn_clicked);

		core.gui.addToCombobox(newgame_gametypecombobox, "easy", GameDifficultyLevel.EASY);
		core.gui.addToCombobox(newgame_gametypecombobox, "normal", GameDifficultyLevel.NORMAL);
		core.gui.addToCombobox(newgame_gametypecombobox, "hard", GameDifficultyLevel.HARD);

		core.gui.comboboxSelectItem(newgame_gametypecombobox, GameDifficultyLevel.NORMAL);

		cy = new CameraYaw(core.rendersystem.getCamera()/*,yaw_speed*/);
	}

	void initialize() {
		debug writeln("StartMenu initialize");

		core.gui.add(startmenu_window);

		state = State.INITED;

		mouseshown = core.gui.showMouseCursor();
	}

	void update(real elapsedTime) {
		//		debug writeln("Intro update");

		if(state != State.INITED && state != State.RUNNING) {
			debug writeln("StartMenu current state is ", state);
			throw new Exception("StartMenu is in invalid state to run.");
		}

		if(state == State.INITED) {
			// épphogy betöltődtünk, indulhat a móka
			state = State.RUNNING;
			runstate = RunState.MAINMENU;

			cy.run();
		}

		if(state == State.RUNNING) {
			cy.update(elapsedTime);
		}

	}

	void pause() {
		cy.stop();
	}

	void restart() {
		cy.run();
	}

	void deinitialize() {
		debug writeln("StartMenu deinitialize");

		cy.stop();

		core.gui.remove(startmenu_window);
		core.gui.showMouseCursor(mouseshown);

		// screen elements
		state = State.DEINITED;
		runstate = RunState.NONE;
	}

	// new singleplayer game screen started
	bool newgame_clicked(void *p) {
		debug writeln("Startmenu newgame button clicked.");

		core.gui.remove(startmenu_window);
		core.gui.add(newgame_window);

		runstate = RunState.NEWGAME;
		return false;
	}

	// new multiplayer game screen started
	bool newmultigame_clicked(void *p) {
		debug writeln("Startmenu newmultigame button clicked.");

		runstate = RunState.NEWMULTIGAME;
		return false;
	}

	// game loading screen started
	bool loadgame_clicked(void *p) {
		debug writeln("Startmenu loadgame button clicked.");

		runstate = RunState.LOAD;
		return false;
	}

	// options screen started
	bool options_clicked(void *p) {
		debug writeln("Startmenu options button clicked.");

		runstate = RunState.OPTIONS;
		return false;
	}
	bool exit_clicked(void *p) {
		debug writeln("Startmenu exit button clicked.");
		core.toGameState(eGameStates.STARTMENU, eGameStates.EXIT);
		return false;
	}

	bool newgame_backbtn_clicked(void *p) {
		debug writeln("NewGame back button clicked.");

		core.gui.remove(newgame_window);
		core.gui.add(startmenu_window);

		runstate = RunState.MAINMENU;
		return false;
	}

	// single user game start
	bool newgame_startgamebtn_clicked(void *p) {
		debug writeln("NewGame startgame button clicked.");

		core.gui.remove(newgame_window);

		uint difficulty = core.gui.getComboboxSelectedItem(newgame_gametypecombobox);
		if (difficulty==-1) difficulty = GameDifficultyLevel.DEFAULT;

		auto client = new Client(core, new SingleUserGameServer(core));
		core.client = client;
		client.createNewGame(core.gui.getWidgetText(newgame_nameeditbox)
							 , core.gui.getWidgetText(newgame_seededitbox)
							 , cast(GameDifficultyLevel)(difficulty)
							 , core.gui.getProp(newgame_violencecheckbox,"Selected")=="True"?false:true);
		core.toGameState(eGameStates.STARTMENU, eGameStates.INGAME);

		return false;
	}

private:

	iCore core;

	enum State {
		CREATED,
		INITED,
		RUNNING,
		DEINITED
	};

	enum RunState {
		NONE,
		MAINMENU,
		NEWGAME,
		NEWMULTIGAME,
		LOAD,
		OPTIONS
	};

	State state;
	RunState runstate;

	Timer timer;

	Window startmenu_window,
			newgame_window;

	bool mouseshown;

	CameraYaw cy;
};
